*** This code predicts tax liability using TAXSIM
*
* Step 2. filing status :: T
*         dependents    :: T

cap log close
set more off
set type double
set linesize 150

adopath + "/home/projects/ota/ado/"
log using "${logdir}/get_data/post_sas/cdw_taxsim_4_yearT_filstat_deps.log" , replace

*NOTE: TAXSIM will appropriately censor net STCG+LTCG at -3000


***********************************
* Load data and prepare variables *
***********************************

use cdw* prim_tin soi*yob nonfiler soi_fil_stat soi_dep* soi_chld_dep_care_credit soi_ctc* soi_eitc ///
	using "${statadir}/pre_taxsim_data.dta" , clear

gen byte TEMP_bad_fil_stat = !inrange(soi_fil_stat,1,4)

sort prim_tin TEMP_bad_fil_stat , stable
by   prim_tin: drop if _n>1

replace soi_fil_stat = 1 if TEMP_bad_fil_stat==1

gen year = 2019

gen state = 0
*NOTE: state=0 means TAXSIM will not calculate state rates

gen     mstat = 1 if soi_fil_stat==1 | soi_fil_stat==4
replace mstat = 2 if soi_fil_stat==2
replace mstat = 6 if soi_fil_stat==3
*mstat :: filing status: 1=single/hoh, 2=mfj, 6=mfs, 8=dependent taxpayer

gen     page = year - soi_prim_yob
replace page = 36 if page>114
*page :: primary age
*TAXSIM returns an error if age exceeds 114

gen     sage = year - soi_sec_yob if mstat==2
replace sage = 36                 if mstat==2 & sage>114
replace sage = 0                  if mstat!=2
*sage :: secondary age
*TAXSIM returns an error if age exceeds 114

gen depx = 0
*depx :: number of dependents

gen dep13 = 0
*dep13 :: number of children under 13 with eligible child care expenses (Dependent Care Credit)

gen dep17 = soi_deps_tot_ctc
*dep17 :: Number of children under 17 for the entire tax year (Child Credit)

gen dep18 = 0
*dep18 :: number of EITC-qualifying children (typically younger than 19 or younger than 24 and a full-time student)

gen dep17_alt = 0
gen dep18_alt = 0
*these will measure the number of children under 17 and 19, regardless of CTC/EITC eligibility

foreach xx of numlist 1/3 {
	replace dep18 = dep18 + 1 if (soi_dep`xx'_eitc_unmasked_tin>0 & !missing(soi_dep`xx'_eitc_unmasked_tin))
}

foreach xx of numlist 1/10 {
	replace depx      = depx      + 1 if soi_dep`xx'_yob>0 & !missing(soi_dep`xx'_yob)
	replace dep13     = dep13     + 1 if soi_chld_dep_care_credit>0 & (2019-soi_dep`xx'_yob)<=13
	replace dep17_alt = dep17_alt + 1 if (2019- soi_dep`xx'_yob)<=16
	replace dep18_alt = dep18_alt + 1 if (2019- soi_dep`xx'_yob)<=18
}

replace dep17 = dep17_alt if soi_ctc_refundable==0 & soi_ctc_nonrefundable
replace dep18 = dep18_alt if soi_eitc==0
replace dep18 = dep17     if dep17>dep18 & dep18==3
replace dep18 = depx      if dep18>depx
replace dep17 = dep18     if dep17>dep18
replace dep13 = dep17     if dep13>dep17
*NOTE: TAXSIM is dumb and requires that dep13<=dep17<=dep18<=depx
*      we deal with this as follows:
*
*      1st we replace dep17/dep18 w/ the counts of children under those ages regardless of credit eligibility, if there is no claimed credit
*      (the idea here is that SOI codes these as zero when there is too much income to receive the credit)
*
*      2nd we replace dep18 with dep17 if dep17 is larger and dep18==3
*      (for EITC claimants, dep18 is censored at 3 bc only 3 children are relevant for EITC.)
*
*      3rd we.. (i)   replace dep18 with depx  if depx  is smaller
*                (ii)  replace dep17 with dep18 if dep18 is smaller
*                (iii) replace dep13 with dep17 if dep17 is smaller
*      for the third step, if there is a "discrepancy" between two variables, we choose the smaller amount of dependents

gen dep6  = dep13
gen dep19 = dep18
* dep6/dep19 :: ???

gen pwages = cdw_wages_prim
*pwages :: primary wages

gen     swages = cdw_wages_sec if mstat==2
replace swages = 0             if mstat!=2
*swages :: secondary wages

gen dividends = max(0,cdw_qdiv)
*dividends :: qualified dividends

gen intrec = cdw_intrec
*intrec :: interest received

gen stcg = cdw_stcg - cdw_st_loss_cf
*stcg :: short-term capital gains or losses

gen ltcg = cdw_ltcg - cdw_lt_loss_cf
*ltcg :: long-term capital gains or losses

gen otherprop = cdw_otherprop
*otherprop :: other income subject to NIIT, including...
*             passive pass-through income,
*             rent not eligible for QBI deduction,
*             non-qualified dividends, etc.

gen nonprop = cdw_nonprop
*nonprop :: other income NOT subject to NIIT, including...
*           alimony,
*           nonwage fellowships,
*           state income tax refunds (itemizers only), etc.
*
*           the following can be included in NONPROP as negative income:
*           alimony paid,
*           Keogh and IRA contributions,
*           foreign income exclusion,
*           NOLs, etc.

gen pensions = cdw_pensions
*pensions :: taxable pensions and IRA distributions

gen gssi = cdw_gssi
*gssi :: gross social security benefits

gen  ui = cdw_ui
gen pui = cdw_ui
*ui/pui :: unemployment compensation received (assumed to be for the primary filer)

gen transfers = 0
*transfers :: other non-taxable transfer income, including...
*             welfare,
*             worker's comp,
*             veterans benefits,
*             child support that would affect eligibility for state property tax rebates but would not be taxable at the federal level, etc.

gen rentpaid = 0
*rentpaid :: rent paid (used only for calculating state property tax rebates)

gen proptax = 0
*proptax :: real estate taxes paid. this is an AMT preference and is also used to calculate state property tax rebates

gen otheritem = 0
*otheritem :: other itemized deductions that are AMT preferences, including...
*             other state and local taxes (line 8 of Sched A) plus local income tax,
*             preference share of medical expenses,
*             miscellaneous (line 27 of Sched A)

gen childcare = 0
*childcare :: childcare expenses

gen mortgage = 0
*mortgage :: deductions not included in OTHERITEM and not a preference for the AMT, including (on Sched A for 2009)...
*            deductible medical expenses not included in Line 16
*            motor vehicle taxes paid (line 7)
*            home mortgage interest (line 15)
*            charitable contributions (line 19)
*            casulty or theft losses (line 20)

gen scorp = 0
*scorp :: active s-corp SSTB income

gen TEMP_pbusinc = cdw_businc_prim
*pbusinc :: primary's non-SSTB QBI (w/ sufficient capital/wages to not phase out)
*           subject to SECA and Additional Medicare Tax

gen     TEMP_sbusinc = cdw_businc_sec if mstat==2
replace TEMP_sbusinc = 0              if mstat!=2
*sbusinc :: secondary's non-SSTB QBI (w/ sufficient capital/wages to not phase out)
*           subject to SECA and Additional Medicare Tax

gen pbusinc = TEMP_pbusinc
gen sbusinc = TEMP_sbusinc

replace pbusinc = 0                           if TEMP_pbusinc<0
replace sbusinc = TEMP_pbusinc + TEMP_sbusinc if TEMP_pbusinc<0

replace pbusinc = TEMP_pbusinc + TEMP_sbusinc if TEMP_sbusinc<0
replace sbusinc = 0                           if TEMP_sbusinc<0

replace pbusinc = 0 if (TEMP_pbusinc + TEMP_sbusinc)<0
replace sbusinc = 0 if (TEMP_pbusinc + TEMP_sbusinc)<0
*NOTE: TAXSIM fails to run when either individual has negative non-SSTB QBI
*      in such cases, if the sum of primary + secondary non-SSTB QBI is negative, we replace both with zeros
*      if the sum is nonnegative, we assign that sum to the one w/ positive non-SSTB QBI

gen pprofinc = 0
*pprofinc :: primary's SSTB QBI
*            subject to SECA and Additional Medicare Tax

gen sprofinc = 0
*sprofinc :: secondary's SSTB QBI
*            subject to SECA and Additional Medicare Tax

gen mtr = 0
*no idea why we need this, but it can't be missing values

drop TEMP* soi*yob *_alt

qui do ${postdir}/taxsim_student_interest.do



*******************************
* Run TAXSIM and save results *
*******************************

gen taxsimid = _n

preserve
	keep taxsimid mstat cdw_sav_cred_base depx dep17
	tempfile savers_credit_vars
	save `savers_credit_vars' , replace
restore

*NOTE: FICA calculations include the employer portion

cd $gdir
compress
taxsimlocal32 , replace full

merge 1:1 taxsimid using `savers_credit_vars' , nogen assert(match)

drop taxsimid

qui do ${postdir}/taxsim_savers_credit.do

local vv 4

rename fica   taxsim`vv'_fica
rename ficar  taxsim`vv'_mtr_fica
rename fiitax taxsim`vv'_fed_incm_tax
rename frate  taxsim`vv'_mtr_ordinc
rename v10    taxsim`vv'_agi
rename v18    taxsim`vv'_txbl_incm
rename v22    taxsim`vv'_ctc
rename v23    taxsim`vv'_addl_ctc
rename v24    taxsim`vv'_childcare_credit
rename v25    taxsim`vv'_eitc
rename v28    taxsim`vv'_incm_tax_b4_credits
rename v42    taxsim`vv'_qbi_ded

keep prim_tin nonfiler taxsim*

duplicates drop

compress
save "${statadir}/taxsim_output_2019_`vv'_filstat_deps" , replace


cap log close


